package dji.ux.ext.setting.panel;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

import dji.common.bus.UXSDKEventBus;
import dji.thirdparty.rx.android.schedulers.AndroidSchedulers;
import dji.thirdparty.rx.subscriptions.CompositeSubscription;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.ext.base.UXEEvents;

public class SettingPagerPanel extends ConstraintLayoutWidget implements View.OnClickListener {

    private ViewPager vp;
    private List<View> views;
    private ImageView menuAircraft;
    private ImageView menuVps;
    private ImageView menuRc;
    private ImageView menuHD;
    private ImageView menuBattery;
    private ImageView menuGimbal;
    private TextView tvTitle;

    private CompositeSubscription subscription = new CompositeSubscription();


    public SettingPagerPanel(@NonNull Context context) {
        super(context);
    }

    public SettingPagerPanel(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SettingPagerPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_setting, this);
        menuAircraft = view.findViewById(R.id.menu_aircraft);
        menuVps = view.findViewById(R.id.menu_vps);
        menuRc = view.findViewById(R.id.menu_rc);
        menuBattery = view.findViewById(R.id.menu_battery);
        menuHD = view.findViewById(R.id.menu_hd);
        menuGimbal = view.findViewById(R.id.menu_gimbal);
        tvTitle = view.findViewById(R.id.title);

        initViews();
        addClickListener();
        initViewPager(view);

        ImageView ivClose = view.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(view1 -> setVisibility(GONE));
    }

    private void initViews() {
        View view1 = View.inflate(getContext(), R.layout.ext_panel_setting_aircraft, null);
        View view2 = View.inflate(getContext(), R.layout.ext_panel_setting_vps, null);
        View view3 = View.inflate(getContext(), R.layout.ext_panel_setting_rc, null);
        View view4 = View.inflate(getContext(), R.layout.ext_panel_setting_hd, null);
        View view5 = View.inflate(getContext(), R.layout.ext_panel_setting_battery, null);
        View view6 = View.inflate(getContext(), R.layout.ext_panel_setting_gimbal, null);
        views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);
        views.add(view5);
        views.add(view6);
    }

    private void addClickListener(){
        menuAircraft.setOnClickListener(this);
        menuVps.setOnClickListener(this);
        menuRc.setOnClickListener(this);
        menuHD.setOnClickListener(this);
        menuBattery.setOnClickListener(this);
        menuGimbal.setOnClickListener(this);
    }

    private void initViewPager(View view){
        vp = view.findViewById(R.id.vp);
        SettingPagerAdapter adapter = new SettingPagerAdapter(views);
        vp.setAdapter(adapter);

        menuAircraft.setBackgroundColor(Color.BLUE);
        vp.setCurrentItem(0);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        int index = 0;
        String title = "";
        if (id == R.id.menu_aircraft) {
            title = "飞控参数设置";
        } else if (id == R.id.menu_vps) {
            index = 1;
            title = "感知避障设置";
        } else if (id == R.id.menu_rc) {
            index = 2;
            title = "遥控器设置";
        } else if (id == R.id.menu_hd) {
            index = 3;
            title = "图传设置";
        }else if (id == R.id.menu_battery) {
            index = 4;
            title = "智能电池设置";
        }else if (id == R.id.menu_gimbal) {
            index = 5;
            title = "云台设置";
        }

        vp.setCurrentItem(index);
        resetImageBg();
        setSelectedBg(index);
        tvTitle.setText(title);
    }

    private void resetImageBg(){
        menuAircraft.setBackgroundColor(Color.TRANSPARENT);
        menuVps.setBackgroundColor(Color.TRANSPARENT);
        menuRc.setBackgroundColor(Color.TRANSPARENT);
        menuHD.setBackgroundColor(Color.TRANSPARENT);
        menuBattery.setBackgroundColor(Color.TRANSPARENT);
        menuGimbal.setBackgroundColor(Color.TRANSPARENT);
    }

    private void setSelectedBg(int index){
        switch (index){
            case 0:
                menuAircraft.setBackgroundColor(Color.BLUE);
                break;
            case 1:
                menuVps.setBackgroundColor(Color.BLUE);
                break;
            case 2:
                menuRc.setBackgroundColor(Color.BLUE);
                break;
            case 3:
                menuHD.setBackgroundColor(Color.BLUE);
                break;
            case 4:
                menuBattery.setBackgroundColor(Color.BLUE);
                break;
            case 5:
                menuGimbal.setBackgroundColor(Color.BLUE);
                break;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        this.subscription.add(UXSDKEventBus.getInstance()
                .register(UXEEvents.SettingPagerEvent.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(pagerEvent -> {
                    int index = pagerEvent.getIndex().ordinal();
                    resetImageBg();
                    setSelectedBg(index);
                    vp.setCurrentItem(index);
                    setVisibility(VISIBLE);
                }));
    }

    @Override
    protected void onDetachedFromWindow() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            this.subscription.unsubscribe();
        }

        super.onDetachedFromWindow();
    }


    @Override
    protected void reactToModelChanges() {

    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }


}
